
package com.ymx.sdk.widget.recyclerview;

import com.ymx.sdk.widget.recyclerview.viewholder.AbsViewHolder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class ListRecyclerAdapter<T> extends RecyclerView.Adapter<AbsViewHolder> {
    public ArrayList<T> mList = new ArrayList<T>();
    private OnItemClickListener mListener;
    private OnItemLongClickListener mLongListener;
    public Context mContext;

    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void OnItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mLongListener = listener;
    }

    public ListRecyclerAdapter(ArrayList<T> datas, Context context) {
        if (datas == null)
            datas = new ArrayList<>();
        this.mList = datas;
        this.mContext = context;
    }

    @Override
    public AbsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return getItemViewHoler(parent, viewType);

    }

    @Override
    public void onBindViewHolder(final AbsViewHolder holder, int position) {
        Object data = mList.get(position);
        holder.bindItemData(data);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    int pos = holder.getPosition();
                    mListener.OnItemClick(v, pos);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mLongListener != null) {
                    int pos = holder.getPosition();
                    mLongListener.OnItemLongClick(v, pos);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            mList = new ArrayList<>();
        return mList.size();
    }

    // 删除指定的Item
    public void removeItemByPosition(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }

    // 在指定位置添加一个新的Item
    public void addItemByPosition(int positionToAdd, T data) {
        mList.add(positionToAdd, data);
        notifyItemInserted(positionToAdd);
    }

    // notify全部Item改变
    public void notifyAllItem() {
        if (mList.size() > 0) {
            notifyItemRangeChanged(0, mList.size() - 1);
        }
    }

    // 在最后面添加多条Items
    public void addItemsInLast(List<T> items) {
        if (items == null)
            return;
        this.mList.addAll(mList.size(), items);
        this.notifyItemRangeInserted(mList.size() - items.size(), items.size());
    }

    // 刷新数据替换新的Items
    public void refreshItems(List<T> items) {
        if (items == null)
            return;
        this.mList.clear();
        this.mList.addAll(items);
        this.notifyDataSetChanged();
    }

    // 根据具体位置获取Item
    public Object getItemDataByPosition(int position) {
        return mList.get(position);
    }

    public ArrayList<T> getList() {
        return mList;
    }

    public void setList(T[] list) {
        ArrayList<T> arrayList = new ArrayList<T>(list.length);
        for (T t : list) {
            arrayList.add(t);
        }
        setList(arrayList);
    }

    public void setList(ArrayList<T> list) {
        if (list == null)
            list = new ArrayList<>();
        mList = list;
        notifyDataSetChanged();
    }

    public View getItemView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext())
                .inflate(getItemLayoutResId(), parent, false);
    }

    @Override
    public void onViewRecycled(AbsViewHolder holder) {
        super.onViewRecycled(holder);
    }

    /**
     * @return item layout id
     */
    public abstract int getItemLayoutResId();

    /**
     * item ViewHolder
     *
     * @param parent
     * @param viewType
     * @return new AbsViewHolder(getItemView(parent, viewType), mContext);
     */
    public abstract AbsViewHolder getItemViewHoler(ViewGroup parent, int viewType);

}
